;var module = angular.module('CtrlsContent');
module.controller('TikuContentCtrl',['$scope','$filter','$state','api_other','$timeout','$stateParams','zuotijilv_service',function($scope,$filter,$state,api_other,$timeout,$stateParams,zuotijilv_service){
	$scope.href = "zhangjieList";
	$scope.totals = $stateParams.totals
	$scope.contentModel = {
		model:{},
		zhangjieContent:function(page){
			if(page == "undefined" || !page){
				page = 1;
			}
			var self = this;
			//记录当前用户章节下分类的当前题数
			localStorage['bls_zj_'+localStorage.bls_userId+localStorage.zhangjieCatId] = page;
			var obj = {
				catId:localStorage.zhangjieCatId,
				userId:localStorage.bls_userId,
				page:page
			};
			var promise = api_other.testContent(obj);
			promise.success(function(data){
				if(data.returnCode == 0){
					self.model = {};
					self.model.options = [];
					self.model = data.returnData[0];
					zuotijilv_service.set(self.model.id,$scope.statusFlag.type);
					$scope.event.getBijiList(self.model.id);
				}else{
					localStorage['bls_zj_'+localStorage.bls_userId+localStorage.zhangjieCatId] = 1;
					alert(data.returnMsg);
					$state.go('zhangjieList',{type:1});
				}
			});
			return promise;
		},
		zhentiContent:function(page){
			if(page == "undefined" || !page){
				page = 1;
			}
			var self = this;
			//记录当前用户章节下分类的当前题数
			localStorage['bls_zt_'+localStorage.bls_userId+localStorage.zhangjieCatId] = page;
			var obj = {
				catId:localStorage.zhangjieCatId,
				userId:localStorage.bls_userId,
				page:page
			};
			var promise = api_other.zhentiContent(obj);
			promise.success(function(data){
				if(data.returnCode == 0){
					self.model = {};
					self.model.options = [];
					self.model = data.returnData[0];
					zuotijilv_service.set(self.model.id,$scope.statusFlag.type);
					$scope.event.getBijiList(self.model.id);
				}else{
					localStorage['bls_zt_'+localStorage.bls_userId+localStorage.zhangjieCatId] = 1;
					alert(data.returnMsg);
					$state.go('zhangjieList',{type:2});
				}
			});
			return promise;
		},
		wrongContent:function(page){
			if(page == "undefined" || !page){
				page = 1;
			}
			var self = this;
			//记录当前用户章节下分类的当前题数
			localStorage['bls_ct_'+localStorage.bls_userId+localStorage.zhangjieCatId] = page;
			var obj = {
				catId:localStorage.zhangjieCatId,
				userId:localStorage.bls_userId,
				page:page,
				type:$stateParams.type
			};
			var promise = api_other.wrongContent(obj);
			promise.success(function(data){
				if(data.returnCode == 0){
					self.model = {};
					self.model.options = [];
					self.model = data.returnData[0];
					$scope.event.getBijiList(self.model.id);
				}else{
					localStorage['bls_ct_'+localStorage.bls_userId+localStorage.zhangjieCatId] = 1;
					alert(data.returnMsg);
					$state.go('zhangjieList',{type:4});
				}
			});
			return promise;
		},
		collectContent:function(page){
			if(page == "undefined" || !page){
				page = 1;
			}
			var self = this;
			//记录当前用户章节下分类的当前题数
			localStorage['bls_sc_'+localStorage.bls_userId+localStorage.zhangjieCatId] = page;
			var obj = {
				catId:localStorage.zhangjieCatId,
				userId:localStorage.bls_userId,
				page:page,
				type:$stateParams.type
			};
			var promise = api_other.shoucangContent(obj);
			promise.success(function(data){
				if(data.returnCode == 0){
					self.model = {};
					self.model.options = [];
					self.model = data.returnData[0];
					$scope.event.getBijiList(self.model.id);
				}else{
					localStorage['bls_sc_'+localStorage.bls_userId+localStorage.zhangjieCatId] = 1;
					alert(data.returnMsg);
					$state.go('zhangjieList',{type:3});
				}
			});
			return promise;
		}
	};
	$scope.statusFlag = {
		page:1,
		answerShow:false,
		resolveShow:false,
		moreSelect:[],
		
		singleSelect:"",
		collect:false,
		beisong:false,

		treeNum:0,	//免费题数
		param:$stateParams.param,
		type:$stateParams.type,

		jiucuoshow:false,
		jiucuoType:1,
		jiucuoContent:'',
		maskshow:false
	};
	$scope.event = {
		init:function(){
			if($stateParams.param == 1){
				var page = (localStorage['bls_zj_'+localStorage.bls_userId+localStorage.zhangjieCatId] || 1);
				$scope.statusFlag.page = page;
				$scope.contentModel.zhangjieContent(page);					
			}else if($stateParams.param == 2){
				var page = (localStorage['bls_zt_'+localStorage.bls_userId+localStorage.zhangjieCatId] || 1);
				$scope.statusFlag.page = page;
				$scope.contentModel.zhentiContent(page);
			}else if($stateParams.param == 3){
				var page = (localStorage['bls_sc_'+localStorage.bls_userId+localStorage.zhangjieCatId] || 1);
				$scope.statusFlag.page = page;
				$scope.contentModel.collectContent(page);
			}else{
				var page = (localStorage['bls_ct_'+localStorage.bls_userId+localStorage.zhangjieCatId] || 1);
				$scope.statusFlag.page = page;
				$scope.contentModel.wrongContent(page);
			}
		},
		bijiList:function(id){
			$state.go('bijiList',{questionId:id,param:$stateParams.param,type:$stateParams.type,totals:$stateParams.totals});
		},
		addBiji:function(id){
			$state.go('biji',{questionId:id,param:$stateParams.param,type:$stateParams.type,totals:$stateParams.totals});
		},
		back:function(){
			$state.go('zhangjieList',{type:$stateParams.param});
		},
		preQuestion:function(){
			if($scope.statusFlag.page == 1){
				alert('当前已经是第一题')
				return;
			}
			$scope.statusFlag.moreSelect = [];
			$scope.statusFlag.page--;
			$scope.statusFlag.answerShow = false;
			$scope.statusFlag.resolveShow = false;
			$scope.statusFlag.beisong = false;
			if($scope.statusFlag.page <=1){
				$scope.statusFlag.page =1;
			}
			if($stateParams.param == 1){
				$scope.contentModel.zhangjieContent($scope.statusFlag.page);
			}else if($stateParams.param == 2){
				$scope.contentModel.zhentiContent($scope.statusFlag.page);
			}else if($stateParams.param == 3){
				$scope.contentModel.collectContent($scope.statusFlag.page);
			}else{
				$scope.contentModel.wrongContent($scope.statusFlag.page);
			}
			
		},
		nextQuestion:function(){
			if($scope.statusFlag.page == $scope.totals){
				alert('当前已经是最后一道题')
				return;
			}
			$scope.statusFlag.moreSelect = [];
			$scope.statusFlag.page++;
			$scope.statusFlag.answerShow = false;
			$scope.statusFlag.resolveShow = false;
			$scope.statusFlag.beisong = false;
			if($stateParams.param == 1){
				$scope.contentModel.zhangjieContent($scope.statusFlag.page);
			}else if($stateParams.param == 2){
				$scope.contentModel.zhentiContent($scope.statusFlag.page);
			}else if($stateParams.param == 3){
				$scope.contentModel.collectContent($scope.statusFlag.page);
			}else{
				$scope.contentModel.wrongContent($scope.statusFlag.page);
			}
		},
		moreSelect:function(key){
			if($scope.statusFlag.answerShow)
				return ;
			if($scope.statusFlag.moreSelect.toString().indexOf(key) ==-1){
				$scope.statusFlag.moreSelect.push(key);
				$scope.statusFlag.moreSelect.sort();
				//$filter('orderBy')($scope.statusFlag.moreSelect, '+');
			}else{
				$scope.statusFlag.moreSelect.splice($scope.statusFlag.moreSelect.toString().replace(/,/g,'').indexOf(key),1);
			}
		},
		moreSelectResolve:function(){
			if($scope.statusFlag.moreSelect.length==0){
				alert('请选择答案');
				return ;
			}
			$scope.statusFlag.answerShow=true;
			var flag = this.isTrue();
			if(flag){
			}else{
				var promise = api_other.wrongSubmit({
					catId:localStorage.zhangjieCatId,
					questionId:$scope.contentModel.model.id,
					userId:localStorage.bls_userId,
					type:$stateParams.type

					//answer:$scope.statusFlag.moreSelect.toString().replace(/,/g,''),
				});				
			}
		},
		singleSelect:function(key){
			if($scope.statusFlag.answerShow)
				return ;
			$scope.statusFlag.singleSelect = key;
			var flag = this.isTrue();
			if(flag){
			}else{
				var promise = api_other.wrongSubmit({
					catId:localStorage.zhangjieCatId,
					questionId:$scope.contentModel.model.id,
					userId:localStorage.bls_userId,
					type:$stateParams.type
				});
			}
			$timeout(function(){
				$scope.statusFlag.answerShow=true; 
			},10);
		},
		isTrue:function(){
			var self = this;
			if($scope.contentModel.model.type == "2"){
				if($scope.contentModel.model.answer == $scope.statusFlag.moreSelect.toString().replace(/,/g,'')){
					$timeout(function(){
						self.nextQuestion();
					},3000);
					return true
				}else{
					return false
				}
			}else{
				if($scope.contentModel.model.answer == $scope.statusFlag.singleSelect){
					$timeout(function(){
						self.nextQuestion();
					},3000);
					return true
				}else{
					return false
				}
			}
		},
		collect:function(){ 
			var obj = {
				userId:localStorage.bls_userId,
				questionId:$scope.contentModel.model.id,
				catId:localStorage.zhangjieCatId,
				type:$stateParams.type
			};
			var promise = api_other.collectTimu(obj);
			promise.success(function(data){
				if(data.returnCode == 0){
					$scope.statusFlag.collect = true;
					$timeout(function(){
						$scope.statusFlag.collect = false;
					},2000);
				}
			});
		},
		getBijiList:function(questionId){
			var obj = {
				questionId:questionId,
				page:1,
				userId:localStorage.bls_userId,
				type:$stateParams.type
			};
			var pro = api_other.bijiList(obj);
			pro.success(function(data){
				if(data.returnCode == 0){
					$scope.bijiModel = data.returnData[0];
				}else{
					$scope.bijiModel = undefined;
				}
			});

		},
		jiucuo:function(){
			var obj = {
				questionId:$scope.contentModel.model.id,
				userId:localStorage.bls_userId,
				status:$scope.statusFlag.jiucuoType,
				content:$scope.statusFlag.jiucuoContent,
				type:$stateParams.type
			};
			var pro = api_other.jiucuo(obj);
			pro.success(function(data){
				if(data.returnCode == 0){
				}else{
					alert('提交失败');
				}
			});
			$scope.statusFlag.jiucuoshow = false,
			$scope.statusFlag.maskshow = false
		},
		jiucuoClose:function(){
			$scope.statusFlag.jiucuoshow = false,
			$scope.statusFlag.maskshow = false
		}
	};

	$scope.event.init();
}])